package com.rchat.pocmini.activity.view;

/**
 * Created by ljd on 2017/3/9.
 *
 *
 */

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rchat.pocmini.R;


public class MessageRecordingView extends RelativeLayout {
    public static final int RECORD_CANCEL = 3;
    public static final int RECORD_OK = 4;
    public static final int START_RECORD = 5;
    public static final int START_TIME = 1;
    public static final int STOP_TIME = 2;
    public static int temp = 0;
    private Chronometer chronometer;
    public Handler handler = new Handler()
    {
        public void handleMessage(Message paramAnonymousMessage)
        {
            super.handleMessage(paramAnonymousMessage);
            switch (paramAnonymousMessage.what) {
                case START_RECORD:
                    MessageRecordingView.this.startTime();
                    MessageRecordingView.this.refreshView(0);
                    break;

                case START_TIME:
                    MessageRecordingView.this.recording();
                    break;
                case STOP_TIME:
                    if(paramAnonymousMessage.obj != null){
                        MessageRecordingView.this.stopTime(((Boolean)paramAnonymousMessage.obj).booleanValue());
                    }
                    break;
                case RECORD_CANCEL:
                    if(paramAnonymousMessage.obj != null){
                        MessageRecordingView.this.refreshView(((Integer)paramAnonymousMessage.obj).intValue());
                    }
                    MessageRecordingView.this.recordCancel();
                    break;
                case RECORD_OK:
                    MessageRecordingView.this.refreshView(0);
                    MessageRecordingView.this.recording();
                    break;

                default:

                    break;
            }
        }
    };

    private Context mInstance;
    private ImageView recordingImage;
    private View porLay = null;
    private ProgressBar pro2;
    private ImageView recordCancel;
    private ImageView screenImage;
    private TextView tvTalkRelease;
    private String timeLength = "01:00";

    public MessageRecordingView(Context paramContext) {
        super(paramContext);
        this.mInstance = paramContext;
    }

    public MessageRecordingView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        this.mInstance = paramContext;
    }

    private void recordCancel()
    {
        if (this.recordingImage.getVisibility() != View.INVISIBLE)
            this.recordingImage.setVisibility(View.INVISIBLE);
        if (this.screenImage.getVisibility() != View.INVISIBLE)
            this.screenImage.setVisibility(View.INVISIBLE);
        if (this.recordCancel.getVisibility() != View.VISIBLE)
            this.recordCancel.setVisibility(View.VISIBLE);
        if (this.porLay.getVisibility() != View.INVISIBLE)
            this.porLay.setVisibility(View.INVISIBLE);
        if (!this.tvTalkRelease.getText().equals("松开取消"))
            this.tvTalkRelease.setText("松开取消");
    }

    private void recording()
    {
        if (this.recordingImage.getVisibility() != View.VISIBLE)
            this.recordingImage.setVisibility(View.VISIBLE);
        if (this.screenImage.getVisibility() != View.VISIBLE)
            this.screenImage.setVisibility(View.VISIBLE);
        if (this.recordCancel.getVisibility() != View.INVISIBLE)
            this.recordCancel.setVisibility(View.INVISIBLE);
        if (this.porLay.getVisibility() != View.VISIBLE)
            this.porLay.setVisibility(View.VISIBLE);
        if (!this.tvTalkRelease.getText().equals("移到此处取消"))
            this.tvTalkRelease.setText("移到此处取消");
    }

    private void refreshView(int paramInt)
    {
        if (paramInt > temp)
        {
            this.pro2.setVisibility(View.GONE);
            return;
        }
        this.pro2.setVisibility(View.VISIBLE);
    }

    private void startTime()
    {
        if (getVisibility() != View.VISIBLE)
            setVisibility(View.VISIBLE);
        recording();
        if (this.chronometer != null)
        {
            this.chronometer.setBase(SystemClock.elapsedRealtime());
            this.chronometer.start();
        }
        if ((this.chronometer != null) && (this.chronometer.getOnChronometerTickListener() == null))
            this.chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener()
            {
                public void onChronometerTick(Chronometer paramAnonymousChronometer)
                {
                    if (MessageRecordingView.this.timeLength.equals(paramAnonymousChronometer.getText()))
                        MessageRecordingView.this.registerMessage(STOP_TIME, Boolean.valueOf(false));
                }
            });
    }

    private void stopTime(boolean paramBoolean)
    {
        setVisibility(View.GONE);
        if (this.chronometer != null)
        {
            this.chronometer.stop();
            this.chronometer.setBase(SystemClock.elapsedRealtime());
        }
    }

    public void countSize(byte[] paramArrayOfByte)
    {
        int i = 0;
        for (int j = 0; ; j++)
        {
            if (j >= paramArrayOfByte.length)
            {
                registerMessage(0, Integer.valueOf(Math.abs(i / 100000)));
                return;
            }
            i = (int)(i + Math.abs(Math.pow(paramArrayOfByte[j], 3.0D)));
        }
    }

    public void initChild(){
        this.chronometer = ((Chronometer)findViewById(R.id.chronometer));
        this.recordingImage = ((ImageView)findViewById(R.id.iv_recording));
        this.screenImage = ((ImageView)findViewById(R.id.iv_screen));
        this.recordCancel = ((ImageView)findViewById(R.id.cancel));
        this.tvTalkRelease = ((TextView)findViewById(R.id.tv_release));
        this.pro2 = ((ProgressBar)findViewById(R.id.progress_small));
        this.porLay = findViewById(R.id.pro_lay);
    }

    public void registerMessage(int paramInt, Object paramObject)
    {
        Message localMessage = this.handler.obtainMessage();
        localMessage.arg1 = paramInt;
        localMessage.what = paramInt;
        localMessage.obj = paramObject;
        this.handler.sendMessage(localMessage);
    }

    public void setText(String paramString)
    {
        if (this.tvTalkRelease != null)
            this.tvTalkRelease.setText(paramString);
    }
}
